﻿using System.Collections;

namespace DimensionsHelper.LanguageService.Syntax;

public class MetadataDbColumns : Declaration, IReadOnlyList<MetadataProperty>
{
    private SyntaxList<MetadataProperty>? _properties;

    internal MetadataDbColumns(Identifier name, SyntaxNode? parent) :
        base(SyntaxKinds.MetadataDbColumns, SyntaxToken.Default, name, parent)
    {
    }


    public int Count => _properties?.Count ?? 0;


    public MetadataProperty this[int index]
    {
        get
        {
            if (_properties == null)
            {
                throw new IndexOutOfRangeException(nameof(index));
            }

            return _properties[index];
        }
    }


    public IEnumerator<MetadataProperty> GetEnumerator()
    {
        return _properties?.GetEnumerator() ??
               Enumerable.Empty<MetadataProperty>().GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }


    public void Add(MetadataProperty property)
    {
        AddChildMember(ref _properties, property);
    }


    public override void AcceptVisitor<T>(SyntaxNodeVisitor<T> visitor)
    {
        Accept(visitor, NameIdentifier);
        AcceptList(visitor, _properties);
    }
}


public static partial class NodeFactory
{
    public static MetadataDbColumns CreateMetadataDbColumns(SyntaxNode? parent,
        Identifier identifierName, IEnumerable<MetadataProperty>? properties)
    {
        MetadataDbColumns columns = new(identifierName, parent);

        if (properties != null)
        {
            foreach (MetadataProperty prop in properties)
            {
                columns.Add(prop);
            }
        }

        return columns;
    }
}